package com.sky.controller.admin;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/*
 * 菜品管理
 */
@RestController("admindishcontroller")
@RequestMapping("/admin/dish")
@Api(tags="菜品相关接口")
@Slf4j
public class DishController {
		@Autowired
		private DishService dishService;
		@Autowired
		private RedisTemplate redisTemplate;
		/*
		 * 新增菜品
		 */
		@PostMapping
		@ApiOperation("新增菜品")
		public Result save(@RequestBody DishDTO dishDTO)
		{
			//添加日志
			log.info("新增菜品:{}",dishDTO);
			dishService.saveWithFlavor(dishDTO);
			//清理缓存数据
			Long Id = dishDTO.getCategoryId();
			String key = "dish"+Id;
			CleanCache(key);
			return Result.success();
		}
		/*
		 * 实现分页查询
		 */
		@GetMapping("/page")
		@ApiOperation("菜品的分页查询")
		public Result<PageResult> page(DishPageQueryDTO dishdto)
		{
			//记录日志
			log.info("菜品分页查询:{}",dishdto);
			//调用service层进行封装
			PageResult pageResult = dishService.page(dishdto);
			return Result.success(pageResult);
		}
		/*
		 * 菜品批量删除
		 * 
		 */
		@DeleteMapping
		@ApiOperation("菜品批量删除")
		public Result delete(@RequestParam List<Long>ids)
		{
			//添加日志
			log.info("菜品批量删除：{}",ids);
			//调用service
			dishService.deleteBranch(ids);
			//将所有的菜品缓存数据清理掉，所有以dish_开头的key
			CleanCache("dish*");
			return Result.success();
		}
		/*
		 * 修改菜品
		 * 1、回显菜品
		 * 2、修改
		 */
		//回显菜品
		@GetMapping("/{id}")
		@ApiOperation("根据id查询菜品")
		public Result<DishVO> getById(@PathVariable Long id)
		{
			log.info("根据id查询菜品：{}",id);
			DishVO dishVO = dishService.getByIdWithFlavor(id); 
			return Result.success(dishVO);
		}
		/*
		 * 修改餐品
		 */
		@PutMapping
		@ApiOperation("修改菜品")
		public Result update(@RequestBody DishDTO dishDto)
		{
			log.info("修改菜品：参数{}",dishDto);
			//调用service进行封装
			dishService.update(dishDto);
			//将所有的菜品缓存数据清理掉，所有以dish_开头的key
			CleanCache("dish*");
			return Result.success();
		}
		@PostMapping("/status/{Status}")
		@ApiOperation("启售和停售")
		public Result StartOrStop(@PathVariable Integer Status,Long id )
		{
			//日志
			log.info("启售和停售的参数：{},{}",Status,id);
			dishService.StartOrStop(Status,id);
			//将所有的菜品缓存数据清理掉，所有以dish_开头的key
			CleanCache("dish_*");
			return Result.success();
		}
		/**
	     * 根据分类id查询菜品
	     * @param categoryId
	     * @return
	*/
	@GetMapping("/list")
	@ApiOperation("根据分类id查询菜品")
	public Result<List<Dish>> list(Long categoryId){
	    List<Dish> list = dishService.list(categoryId);
	    return Result.success(list);
	}
	public void CleanCache(String key)
	{
		log.info("删除redis的参数：{}",key);
		Set keys = redisTemplate.keys(key);
		redisTemplate.delete(keys);
	}
}
